import Vue from 'vue'
import VueRouter from 'vue-router'

/**
 * 重写路由的push方法
 */
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}
Vue.use(VueRouter)



const routes = [{
        path: "*",
        redirect: "/"
    },
    {
        path: "/",
        redirect: "/index",
    },
    {
        path: "/offline",
        name: "offline",
        component: () =>
            import ("@/views/offline"),
        meta: { nonoRequireAuth: true, closeApp: true, }
    },
    {
        path: "/login",
        name: "login",
        component: () =>
            import ("@/views/login/login"),
        meta: { nonoRequireAuth: true, closeApp: true, }
    },
    {
        path: "/iphoneLogin",
        name: "iphoneLogin",
        component: () =>
            import ("@/views/login/iphoneLogin"),
        meta: { nonoRequireAuth: true }
    },
    {
        path: "/setIphoneLogin",
        name: "setIphoneLogin",
        component: () =>
            import ("@/views/login/setIphoneLogin"),
        meta: { nonoRequireAuth: true }
    },
    {
        path: "/userLogin",
        name: "userLogin",
        component: () =>
            import ("@/views/login/userLogin"),
        meta: { nonoRequireAuth: true }
    },
    {
        path: "/signIn",
        name: "signIn",
        component: () =>
            import ("@/views/login/signIn"),
        meta: { nonoRequireAuth: true, keepAlive: true }
    },
    {
        path: "/setSignIn",
        name: "setSignIn",
        component: () =>
            import ("@/views/login/setSignIn"),
        meta: { nonoRequireAuth: true }
    },
    {
        path: "/forget",
        name: "forget",
        component: () =>
            import ("@/views/login/forget"),
        meta: { nonoRequireAuth: true }
    },
    {
        path: "/setForget",
        name: "setForget",
        component: () =>
            import ("@/views/login/setForget"),
        meta: { nonoRequireAuth: true }
    },
    {
        path: "/rotocol",
        name: "rotocol",
        component: () =>
            import ("@/views/login/rotocol"),
        meta: { nonoRequireAuth: true, keepAlive: true }
    },
    {
        path: "/download",
        name: "download",
        component: () =>
            import ("@/views/login/download"),
        meta: { nonoRequireAuth: true }
    },
    {
        path: "/index",
        component: () =>
            import ("@/layar"),
        children: [{
                path: "/index",
                name: "index",
                component: () =>
                    import ("@/views/index"),
                meta: {
                    keepAlive: true,
                    closeApp: true,
                },
            },
            {
                path: "/walletIndex",
                name: "walletIndex",
                component: () =>
                    import ("@/views/wallet/walletIndex"),
                meta: {
                    keepAlive: true,
                    closeApp: true,
                },
            },
            {
                path: "/consultIndex",
                name: "consultIndex",
                component: () =>
                    import ("@/views/consult/consultIndex"),
                meta: {
                    keepAlive: true,
                    closeApp: true,
                },
            },
            {
                path: "/myCenter",
                name: "myCenter",
                component: () =>
                    import ("@/views/user/myCenter"),
                meta: {
                    keepAlive: true,
                    closeApp: true,
                },
            },
        ],

    },
    {
        path: "/goodsDetail",
        name: "goodsDetail",
        component: () =>
            import ("@/views/index/goodsDetail"),
        meta: {
            keepAlive: true,
        },
    },
    {
        path: "/confirmOrder",
        name: "confirmOrder",
        component: () =>
            import ("@/views/index/confirmOrder"),
    },
    {
        path: "/otherPay",
        name: "otherPay",
        component: () =>
            import ("@/views/index/otherPay"),
    },
    {
        path: "/flashIndex",
        name: "flashIndex",
        component: () =>
            import ("@/views/consult/flashIndex"),
    },
    {
        path: "/articleDetail",
        name: "articleDetail",
        component: () =>
            import ("@/views/consult/articleDetail"),
    },
    {
        path: "/messageList",
        name: "messageList",
        component: () =>
            import ("@/views/index/messageList"),
    },
    {
        path: "/messageInfo",
        name: "messageInfo",
        component: () =>
            import ("@/views/index/messageInfo"),
    },
    {
        path: "/allotT",
        name: "allotT",
        component: () =>
            import ("@/views/wallet/allotT"),
    },
    {
        path: "/returnsDetail",
        name: "returnsDetail",
        component: () =>
            import ("@/views/wallet/returnsDetail"),
    },
    {
        path: "/transferAccount",
        name: "transferAccount",
        component: () =>
            import ("@/views/wallet/transferAccount"),
    },
    {
        path: "/collectionAccount",
        name: "collectionAccount",
        component: () =>
            import ("@/views/wallet/collectionAccount"),
    },
    {
        path: "/exportKey",
        name: "exportKey",
        component: () =>
            import ("@/views/wallet/exportKey"),
    },
    {
        path: "/withdraw",
        name: "withdraw",
        component: () =>
            import ("@/views/wallet/withdraw"),
    },
    {
        path: "/withdrawRecord",
        name: "withdrawRecord",
        component: () =>
            import ("@/views/wallet/withdrawRecord"),
    },
    {
        path: "/walletManage",
        name: "walletManage",
        component: () =>
            import ("@/views/wallet/walletManage"),
    },
    {
        path: "/createWallet",
        name: "createWallet",
        component: () =>
            import ("@/views/wallet/createWallet"),
    },
    {
        path: "/setLoginPass",
        name: "setLoginPass",
        component: () =>
            import ("@/views/setting/setLoginPass"),
    },
    {
        path: "/myOrder",
        name: "myOrder",
        component: () =>
            import ("@/views/user/myOrder"),
        meta: {
            // keepAlive: true,
        },
    },
    {
        path: "/myOrderOnline",
        name: "myOrderOnline",
        component: () =>
            import ("@/views/user/myOrderOnline"),
        meta: {
            // keepAlive: true,
        },
    },
    {
        path: "/myCustoms",
        name: "myCustoms",
        component: () =>
            import ("@/views/user/myCustoms"),
    },
    {
        path: "/trusteeship",
        name: "trusteeship",
        component: () =>
            import ("@/views/user/trusteeship"),
    },
    {
        path: "/myFriend",
        name: "myFriend",
        component: () =>
            import ("@/views/user/myFriend"),
    },
    {
        path: "/myInfo",
        name: "myInfo",
        component: () =>
            import ("@/views/user/myInfo"),
    },
    {
        path: "/myIphone",
        name: "myIphone",
        component: () =>
            import ("@/views/user/myIphone"),
    },
    {
        path: "/myAuthentication",
        name: "myAuthentication",
        component: () =>
            import ("@/views/user/myAuthentication"),
    },
    {
        path: "/myBankcard",
        name: "myBankcard",
        component: () =>
            import ("@/views/user/myBankcard"),
    },
    {
        path: "/aboutUs",
        name: "aboutUs",
        component: () =>
            import ("@/views/user/aboutUs"),
    },
    {
        path: "/mySet",
        name: "mySet",
        component: () =>
            import ("@/views/user/mySet"),
    },
    {
        path: "/amendPass",
        name: "amendPass",
        component: () =>
            import ("@/views/user/amendPass"),
    },
    {
        path: "/setamendPass",
        name: "setamendPass",
        component: () =>
            import ("@/views/user/setamendPass"),
    },
    {
        path: "/payPass",
        name: "payPass",
        component: () =>
            import ("@/views/user/payPass"),
    },
    {
        path: "/resetPaypass",
        name: "resetPaypass",
        component: () =>
            import ("@/views/user/resetPaypass"),
    },
    {
        path: "/myOrderDetail",
        name: "myOrderDetail",
        component: () =>
            import ("@/views/user/myOrderDetail"),
        meta: {
            keepAlive: true,
        },
    },



];
const router = new VueRouter({
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            if (from.meta.keepAlive) {
                from.meta.savedPosition = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
            }
            return { x: 0, y: to.meta.savedPosition || 0 }
        }
    },
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})



router.beforeEach((to, from, next) => {
    let user = JSON.parse(localStorage.getItem('userInfor'))
    if (!to.meta.nonoRequireAuth) {
        if ((user && user.token)) {
            next()
        } else {
            next({ name: 'login' })
        }
    } else {
        next()
    }
})

export default router